<?php

namespace common\repositories\config;

use common\entities\ConfigEntity;
use common\entities\KfsDbEntity;
use common\entities\config\MyscrmFunctionupgradeEntity;
use common\repositories\BaseRepository;
use common\entities\config\PigcmsWxUserEntity;
use common\support\StringHelper;

class MyscrmFunctionupgradeRepository extends BaseRepository
{

    private $_myscrmFunctionupgradeEntity;

    public function __construct()
    {
        $this->_myscrmFunctionupgradeEntity = new MyscrmFunctionupgradeEntity();
    }


    /**
     * 根据organizationId获取数据
     * @param $organizationId
     * @return array|bool|\yii\db\ActiveRecord[]
     */
    public function getDataByOrganizationId($organizationId)
    {
        return $this->_myscrmFunctionupgradeEntity->getDataByOrganizationId($organizationId);
    }

    /**
     * 根据根据organizationId和FunctionName获取数据
     * @param $organizationId
     * @param $functionName
     * @return array|bool|\yii\db\ActiveRecord[]
     */
    public function getOrgFunctionMode($organizationId, $functionName){
        return $this->_myscrmFunctionupgradeEntity->getOrgFunctionMode($organizationId, $functionName);
    }

    /**
     * 判断功能是否升级
     * @param mixed $token
     * @param mixed $functionName
     */
    public function hasUpgrade($token, $functionName)
    {
        $record = PigcmsWxUserEntity::findOne(['token' => $token]);
        if (!$record)
        {
            return false;
        }

        $organizationId = $record['OrganizationId'];
        $record = MyscrmFunctionupgradeEntity::findOne(['organizationId' => $organizationId, 'functionName' => $functionName]);
        return $record && $record['functionMode'] == '2';
    }

    /**
     * 判断当前租户功能是否升级
     * @param string $functionName
     * @return bool
     */
    public function hasCurrentUpgrade($functionName)
    {
        $organizationRepository = new OrganizationRepository;
        $organization = $organizationRepository->getOrgByUniqueName(KfsDbEntity::$orgcode);
        if (empty($organization))
        {
            return false;
        }

        $record = MyscrmFunctionupgradeEntity::findOne(['organizationId' => $organization->Id, 'functionName' => $functionName]);
        return $record && $record['functionMode'] == '2';
    }

    /**
     * 判断当前租户功能是否升级
     * @param string $functionName
     * @return bool
     */
    public function hasOrganizationUpgrade($organizationId, $functionName)
    {
        $record = MyscrmFunctionupgradeEntity::findOne(['organizationId' => $organizationId, 'functionName' => $functionName]);
        return $record && $record['functionMode'] == '2';
    }

    /**
     * 是否升级了拓客中心
     * @param mixed $token
     */
    public function hasTouKeUpgrade($token)
    {
        return $this->hasUpgrade($token, '拓客中心');
    }

    /**
     * 根据租户id与方法名，判断功能是否升级
     * @param mixed $organizationId 租户id
     * @param mixed $functionName 方法名称
     * @return boolean
     * @author wangwx
     */
    public static function isUpgrade($organizationId, $functionName)
    {
        $record = MyscrmFunctionupgradeEntity::findOne(['organizationId' => $organizationId, 'functionName' => $functionName]);
        if ($record && $record['functionMode'] == '2') {
            // 这个逻辑有点反人类啊 —— 吴昊
            return false;
        }else{
            return true;
        }
    }

    /**
     * 获取哪些租户升级了指定的功能
     * @param string $functionName 功能名称
     * @param int $functionMode 功能值
     * @return array|MyscrmFunctionupgradeEntity[]
     */
    public function getUpgradeOrganizations($functionName,$functionMode = 2)
    {
        return MyscrmFunctionupgradeEntity::find()
            ->with('organization')
            ->where(['functionName' => $functionName, 'functionMode' => $functionMode])
            ->all();
    }

    /**
     * 获取升级了功能的租户列表
     * @param string $functionName 功能名称
     * @param string $functionMode 功能值
     * @return array
     */
    public function getOrgListByFunctionNameAndMode($functionName,$functionMode = 2){
        $db = ConfigEntity::getDb();
        $sql = "select fun.*,org.UniqueName,org.FriendlyName
                from myscrm_functionupgrade as fun
                inner join myscrm_organization as org on fun.organizationId = org.Id
                where fun.functionName = :functionName and fun.functionMode = :functionMode and org.State = 1 ";

        return $db->createCommand($sql,[':functionName' => $functionName,':functionMode' => $functionMode])->queryAll();
    }

    /**
     * 获取升级了功能且绑定了ERP的租户列表
     * @param string $functionName 功能名称
     * @param string $functionMode 功能值
     * @return array
     */
    public function getBindErpOrgListByFunctionNameAndMode($functionName,$functionMode = 2){
        $db = ConfigEntity::getDb();
        $sql =
            <<<eof
                select fun.*,org.UniqueName,org.FriendlyName
    from myscrm_functionupgrade as fun
    inner join myscrm_organization as org on fun.organizationId = org.Id
    where fun.functionName = :functionName and fun.functionMode = :functionMode and org.State = 1
    and org.UniqueName in (select org_code from ei_polling_organization where polling_type='2')
eof;


        return $db->createCommand($sql,[':functionName' => $functionName,':functionMode' => $functionMode])->queryAll();
    }

    public function enableFunction($orgId, $functionName)
    {
        $record = MyscrmFunctionupgradeEntity::find()->where(
            [
                'organizationId' => $orgId,
                'functionName'   => $functionName,
            ]
        )->one();
        if (!$record) {
            $record = new MyscrmFunctionupgradeEntity;
            $record->functionUpgradeId = StringHelper::uuid();
            $record->organizationId = $orgId;
            $record->functionName = $functionName;
        }
        $record->functionMode = 2;

        return $record->save();
    }

    public function disableFunction($orgId, $functionName)
    {
        $record = MyscrmFunctionupgradeEntity::find()->where(
            [
                'organizationId' => $orgId,
                'functionName'   => $functionName,
            ]
        )->one();
        if (!$record) {
            $record = new MyscrmFunctionupgradeEntity;
            $record->functionUpgradeId = StringHelper::uuid();
            $record->organizationId = $orgId;
            $record->functionName = $functionName;
        }
        $record->functionMode = 1;

        return $record->save();
    }
}